package com.cf.data.serve.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cf.data.serve.entity.AiBox;
import com.cf.data.serve.entity.AiBoxAlarm;
import com.cf.data.serve.service.IAiBoxAlarmService;
import com.cf.data.serve.service.IAiBoxService;
import com.cf.data.serve.utils.RedisUtil;
import com.cf.data.serve.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author cuixi
 * @description: ai盒子告警数据接口
 * @date 2024/11/2
 */
@RestController
@RequestMapping("/box-api/alarm")
@Slf4j
public class AiBoxController {
    @Autowired
    private IAiBoxAlarmService aiBoxAlarmService;
    @Autowired
    private IAiBoxService aiBoxService;
    @Autowired
    private RedisUtil redisUtil;


    @PostMapping(value = "/custom/router1")
    public Result<?> sendBoxMessage(@RequestBody JSONObject data) {
        log.info("获取ai盒子消息：" + JSON.toJSONString(data));

        //转换实体
        AiBoxAlarm aiBoxAlarm = new AiBoxAlarm();
        //警报类型 上报内容
        //1：行为分析告警数据
        //2：告警恢复数据
        //3：人脸抓拍数据
        //4：人脸比对数据
        //5：行为比对数据
        //6：保活数据
        //7：点位变更数据
        aiBoxAlarm.setType(data.getString("type"));
        //设备信息-----
        JSONObject deviceInfo = data.getJSONObject("device_info");
        //设备信息-编码
        aiBoxAlarm.setDeviceCode(deviceInfo.getString("device_code"));
        //设备信息-设备名称
        aiBoxAlarm.setDeviceName(deviceInfo.getString("device_name"));
        //设备信息-序列号
        aiBoxAlarm.setDeviceSn(deviceInfo.getString("deviceSn"));

        //如果是状态6保活过滤掉
        if ("6".equals(data.getString("type"))) {
            //有则添加设备并修改最后修改时间
            if (!redisUtil.hasKey("ai_box_info")) {
                redisUtil.set("ai_box_info", JSON.toJSONString(aiBoxService.list()),600);
            }
            List<AiBox> boxList = JSONObject.parseArray(redisUtil.get("ai_box_info").toString(), AiBox.class);
            Map<String, AiBox> map = boxList.stream().collect(Collectors.toMap(AiBox::getDeviceCode, aiBox -> aiBox));
            //设备已存在更新存活时间
            if (map.containsKey(aiBoxAlarm.getDeviceCode())) {
                AiBox aiBox = map.get(aiBoxAlarm.getDeviceCode());
                aiBox.setDeviceCode(aiBoxAlarm.getDeviceCode());
                aiBox.setDeviceName(aiBoxAlarm.getDeviceName());
                aiBox.setDeviceSn(aiBoxAlarm.getDeviceSn());
                aiBox.setLastTime(new Date());
                aiBoxService.updateById(aiBox);
            } else {
                //不存在新增
                AiBox aiBox = new AiBox();
                aiBox.setDeviceCode(aiBoxAlarm.getDeviceCode());
                aiBox.setDeviceName(aiBoxAlarm.getDeviceName());
                aiBox.setDeviceSn(aiBoxAlarm.getDeviceSn());
                aiBox.setLastTime(new Date());
                aiBoxService.save(aiBox);
                redisUtil.set("ai_box_info", JSON.toJSONString(aiBoxService.list()),600);
            }
            return Result.ok();
        }
        //有无比对结果 有无比对结果 1 有 0 无
        aiBoxAlarm.setHasResult(data.getInteger("has_result"));
        //通道信息-----
        if (data.containsKey("channel_info")) {
            JSONObject channelInfo = data.getJSONObject("channel_info");
            //点位id
            aiBoxAlarm.setChannelId(channelInfo.getString("channel_id"));
            aiBoxAlarm.setChannelName(channelInfo.getString("channel_id"));
        }

        //行为分析-----
        if (data.containsKey("behaviour")) {
            JSONObject behaviour = data.getJSONObject("behaviour");
            aiBoxAlarm.setImagePath(behaviour.getString("image_path"));
            aiBoxAlarm.setCaptureTime(behaviour.getString("capture_time"));
        }
        //目标信息
//        JSONObject captureInfo = behaviour.getJSONObject("capture_info");

        //人脸抓拍数据-----
        if (data.containsKey("face")) {
            JSONObject face = data.getJSONObject("face");
            aiBoxAlarm.setOrigImagePath(face.getString("orig_image_path"));
            aiBoxAlarm.setCropImagePath(face.getString("crop_image_path"));
        }
        //有比对结果添加人员信息
        if (aiBoxAlarm.getHasResult().equals(1)) {
            if (data.containsKey("compare_results")) {
                JSONObject compareResults = data.getJSONObject("compare_results");
                aiBoxAlarm.setTargetId(compareResults.getString("target_id"));
                aiBoxAlarm.setLibId(compareResults.getString("lib_id"));
                aiBoxAlarm.setLibName(compareResults.getString("lib_name"));
                aiBoxAlarm.setPersonId(compareResults.getString("person_id"));
                aiBoxAlarm.setPersonName(compareResults.getString("person_name"));
                aiBoxAlarm.setSex(compareResults.getString("sex"));
                aiBoxAlarm.setTel(compareResults.getString("tel"));
                aiBoxAlarm.setLibImagePath(compareResults.getString("image_path"));
                aiBoxAlarm.setSimilarity(compareResults.getBigDecimal("similarity"));
            }
        }

        aiBoxAlarmService.save(aiBoxAlarm);
        return Result.ok();
    }
}
